<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // https://leetcode-cn.com/problems/3sum/
        // TODO 明天自己写一遍
        var threeSum = function (nums) {
            // 先进行排序
            nums.sort((a, b) => {
                return a - b
            })
            let res = []
            // 定住第一个指针
            for (let i = 0; i < nums.length; i++) {
                // 先判重
                while (nums[i - 1] === nums[i]) {
                    i++;
                }
                // 确定两个指针
                let p = i + 1;
                let q = nums.length - 1

                while (p < q) {
                    // 开始找
                    let sum = nums[i] + nums[p] + nums[q];
                    if (sum === 0) {
                        res.push([nums[i], nums[p], nums[q]]);
                        p++;
                        while (nums[p - 1] === nums[p]) {
                            p++;
                        }
                        q--;
                        while (nums[q + 1] === nums[q]) {
                            q--;
                        }
                    } else if (sum < 0) {
                        p++;
                        while (nums[p - 1] === nums[p]) {
                            p++;
                        }
                    } else {
                        q--;
                        while (nums[q + 1] === nums[q]) {
                            q--;
                        }
                    }
                }
            }

            return res
        };

        console.log(threeSum([-1,0,1,2,-1,-4]));
    </script>
</body>

</html>